﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TSCC.DataAccess;
using TSCC.Interface;

namespace TSCC.Service
{
    public class Utility
    {
        public static Interface.iUser Login(string p_id, string p_pw)
        {
            DataAccess.Utility ut = new TSCC.DataAccess.Utility();
            System.Data.DataTable dt = ut.GetUserInfo(p_id);
            UserInfo usr = null;
            if (dt.Rows.Count == 1)
            {
                usr = new UserInfo(dt, p_pw);
            }            
            return usr;
        }

        public static List<iCMBItem> GetExceptionList()
        {
            DA_Exception obj = new DA_Exception();
            DataTable DT = obj.GetException();
            List<iCMBItem> itemList = new List<iCMBItem>();
            foreach (DataRow row in DT.Rows)
            {
                itemList.Add(new CMBItem(
                    row["CATEGORY"].ToString(), row["CATEGORY"].ToString()));
            }
            return itemList;
        }

        public static DateTime GetDateTime(string p_time)
        {
            DateTime calTime = DateTime.MinValue;
            try
            {
                string strDate = string.Empty;
                strDate = string.Format("2010-01-01 {0}:{1}"
               , p_time.Substring(0, 2), p_time.Substring(2, 2));
                calTime = Convert.ToDateTime(strDate);                
            }
            catch (Exception ex)
            { 
            
            }
            return calTime;
        }

        public static bool IsTimeFormat(string p_time)
        {
            //string strDate = string.Empty;
            bool ok = true;
            DateTime result = GetDateTime(p_time);
            return result == DateTime.MinValue ? false : true ;
        }

        public static bool EndGreatThanStart(string p_start, string p_end)
        {
            //檢查兩個項目，1.是否為時間格式2.開始不能大於結束

            bool ok = false;
            int start,end;
            start = Convert.ToInt16(p_start);
            end = Convert.ToInt16(p_end);

            if ((IsTimeFormat(p_start)) & (IsTimeFormat(p_end)))
            {
                ok = start > end ? false : true;
                if (start.Equals(end))//也不能等於,否則算出來會是0分
                {
                    ok = false;
                }
            }
            return ok;
        }

        public static List<string> GetTeam()
        {
            TSCC.DataAccess.Utility utility = new TSCC.DataAccess.Utility();
            DataTable dt = utility.GetTeam();
            List<string> itmList = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                itmList.Add(row[0].ToString());
            }
            return itmList;
        }

        public static bool IsDate(string p_date)
        {
            bool ok = true;
            try
            {
                string sDate = GetDateString(p_date);

                Convert.ToDateTime(sDate);
            }
            catch (Exception ex)
            {
                ok = false;
            }
            return ok;
        }

        public static string GetDateString(string p_date)
        {
            return string.Format("{0}-{1}-{2}"
                        , p_date.Substring(0, 4)
                        , p_date.Substring(4, 2)
                        , p_date.Substring(6, 2));

        }
    }
}
